package com.rq.rvlibrary;


import android.text.TextUtils;

import androidx.recyclerview.widget.DiffUtil;

import java.util.List;

public class RQDiffCallBack extends DiffUtil.Callback {
    private List oldDatas, newDatas;

    public RQDiffCallBack(List old, List newD) {
        this.oldDatas = old;
        this.newDatas = newD;
    }

    //老数据集size
    @Override
    public int getOldListSize() {
        return oldDatas != null ? oldDatas.size() : 0;
    }

    //新数据集size
    @Override
    public int getNewListSize() {
        return newDatas != null ? newDatas.size() : 0;
    }

    protected final boolean isOldImplements(int position) {
        try {
            return Compare.class.isAssignableFrom(oldDatas.get(position).getClass());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected final boolean isNewImplements(int position) {
        try {
            return Compare.class.isAssignableFrom(newDatas.get(position).getClass());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        boolean res = false;
        try {
            if (oldDatas.get(oldItemPosition) instanceof Double && newDatas.get(newItemPosition) instanceof Double) {
                res = (double) oldDatas.get(oldItemPosition) == (double) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof Float && newDatas.get(newItemPosition) instanceof Float) {
                res = (float) oldDatas.get(oldItemPosition) == (float) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof Long && newDatas.get(newItemPosition) instanceof Long) {
                res = (long) oldDatas.get(oldItemPosition) == (long) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof Integer && newDatas.get(newItemPosition) instanceof Integer) {
                res = (int) oldDatas.get(oldItemPosition) == (int) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof CharSequence && newDatas.get(newItemPosition) instanceof CharSequence) {
                res = TextUtils.equals((CharSequence) oldDatas.get(oldItemPosition), (CharSequence) newDatas.get(newItemPosition));
            } else if (isOldImplements(newItemPosition) && isNewImplements(newItemPosition)) {
                res = ((Compare) oldDatas.get(oldItemPosition)).areItemsTheSame((Compare) newDatas.get(newItemPosition));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        boolean res = false;
        try {
            if (oldDatas.get(oldItemPosition) instanceof Double && newDatas.get(newItemPosition) instanceof Double) {
                res = (double) oldDatas.get(oldItemPosition) == (double) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof Float && newDatas.get(newItemPosition) instanceof Float) {
                res = (float) oldDatas.get(oldItemPosition) == (float) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof Long && newDatas.get(newItemPosition) instanceof Long) {
                res = (long) oldDatas.get(oldItemPosition) == (long) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof Integer && newDatas.get(newItemPosition) instanceof Integer) {
                res = (int) oldDatas.get(oldItemPosition) == (int) newDatas.get(newItemPosition);
            } else if (oldDatas.get(oldItemPosition) instanceof CharSequence && newDatas.get(newItemPosition) instanceof CharSequence) {
                res = TextUtils.equals((CharSequence) oldDatas.get(oldItemPosition), (CharSequence) newDatas.get(newItemPosition));
            } else if (isOldImplements(newItemPosition) && isNewImplements(newItemPosition)) {
                res = ((Compare) oldDatas.get(oldItemPosition)).areContentsTheSame((Compare) newDatas.get(newItemPosition));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
